//
//  ChangeLawyerViewController.m
//  e律师
//
//  Created by nykj-mac-03 on 2017/6/7.
//  Copyright © 2017年 test. All rights reserved.
//

#import "ChangeLawyerViewController.h"

@interface ChangeLawyerViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,JHPickerDelegate>
{
    UITextField *searchField;
    UILabel *searchType;
    UIButton *searchButton;
    NSString *searchString;
}

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;



@end

@implementation ChangeLawyerViewController
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    //    self.automaticallyAdjustsScrollViewInsets = false;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataSource = [NSMutableArray array];
    self.barTitleLable.text = @"变更律师";
    [self initUI];
    [self getDataSource];
}
- (void)initUI{
    [self.view insertSubview:self.tableView atIndex:0];
}
- (void)getDataSource{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetworkingManager getUrl:[NSString stringWithFormat:@"%@company_id=%@",CHANGELWYER_URL,[InfoSingleTon personInfoSingleTon].infoDic[@"cid"]] dic:nil Succeed:^(id responseData) {
        
        self.dataSource = responseData[@"result"];
        [self.tableView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"%@",self.dataSource);
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络异常"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}
#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    textField.clearsOnBeginEditing = YES;
    searchButton.backgroundColor = [UIColor lightGrayColor];
    searchButton.userInteractionEnabled = NO;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    searchString = textField.text;
    if (textField.text.length == 0) {
        searchButton.backgroundColor = [UIColor lightGrayColor];
        searchButton.userInteractionEnabled = NO;
    }
    else{
        searchButton.backgroundColor = [UIColor colorWithRed:33.0f/255.0f green:180.0f/255.0f blue:237.0f/255.0f alpha:1.0f];
        searchButton.userInteractionEnabled = YES;
    }
    [textField resignFirstResponder];
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    searchString = textField.text;
    if (textField.text.length == 0) {
        searchButton.backgroundColor = [UIColor lightGrayColor];
        searchButton.userInteractionEnabled = NO;
    }
    else{
        searchButton.backgroundColor = [UIColor colorWithRed:33.0f/255.0f green:180.0f/255.0f blue:237.0f/255.0f alpha:1.0f];
        searchButton.userInteractionEnabled = YES;
    }
}

#pragma mark - ButtonTarget
- (void)searchAction:(UIButton *)sender{
    NSLog(@"查询 ");
    NSLog(@"searchStr : %@",searchString);
    NSLog(@"searchType : %@",searchType);
    if ([searchType.text isEqualToString:@"请选择"]) {
        
        [MBProgressHUD showError:@"请选择"];
        
    }
    else if ([searchType.text isEqualToString:@"客户（委托人）搜索"]) {
        NSLog(@"按委托人搜索");
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [NetworkingManager getUrl:SEARCHBAILOR dic:@{@"company_id":[InfoSingleTon personInfoSingleTon].infoDic[@"cid"],@"wtr":searchString} Succeed:^(id responseData) {
            NSLog(@"success :%@",responseData);
            self.dataSource = responseData[@"result"];
            [self.tableView reloadData];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
        } failure:^(NSError *error) {
            NSLog(@"error :%@",error);
            [MBProgressHUD showError:@"网络异常"];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }
    else if ([searchType.text isEqualToString:@"当事人搜索"]){
        NSLog(@"按当事人搜索");
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [NetworkingManager getUrl:SEARCHADVERSARY dic:@{@"company_id":[InfoSingleTon personInfoSingleTon].infoDic[@"cid"],@"dfdsr":searchString} Succeed:^(id responseData) {
            NSLog(@"success :%@",responseData);
            self.dataSource = responseData[@"result"];
            [self.tableView reloadData];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        } failure:^(NSError *error) {
            NSLog(@"error :%@",error);
            [MBProgressHUD showError:@"网络异常"];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }
    else if ([searchType.text isEqualToString:@"案号搜索"]){
        NSLog(@"按案号搜索");
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [NetworkingManager getUrl:SEARCHCASENUMBER dic:@{@"company_id":[InfoSingleTon personInfoSingleTon].infoDic[@"cid"],@"ah_number":searchString} Succeed:^(id responseData) {
            NSLog(@"success :%@",responseData);
            self.dataSource = responseData[@"result"];
            [self.tableView reloadData];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        } failure:^(NSError *error) {
            NSLog(@"error :%@",error);
            [MBProgressHUD showError:@"网络异常"];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }
}
- (void)action_ChooseType:(UIButton *)sender{
    JHPickView *picker = [[JHPickView alloc] initWithFrame:self.view.bounds];
    picker.delegate = self ;
    picker.selectLb.text = @"请选择";
    picker.customArr = @[@"客户（委托人）搜索",@"当事人搜索",@"案号搜索"];
    [self.view addSubview:picker];
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 135;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 60;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChangeLawyerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChangeLawyerCell" forIndexPath:indexPath];
    
    cell.caseNumLable.text = [NSString stringWithFormat:@"案件号： %@",self.dataSource[indexPath.row][@"ah_number"]];
    cell.bailorLable.text = [NSString stringWithFormat:@"委托人： %@",self.dataSource[indexPath.row][@"wtr"]];
    cell.adversaryLable.text = [NSString stringWithFormat:@"对方当事人： %@",self.dataSource[indexPath.row][@"dfdsr"]];
    cell.lawyerName.text = [NSString stringWithFormat:@"律师： %@",self.dataSource[indexPath.row][@"name"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] init];
    
    UIView *searchTextView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 120, 30)];
    searchTextView.layer.borderColor = [UIColor colorWithRed:102.0f/255.0f green:159.0f/255.0f blue:190.0f/255.0f alpha:1.0f].CGColor;
    searchTextView.layer.borderWidth = 1.0f;
    searchTextView.layer.cornerRadius = 5;
    searchTextView.layer.masksToBounds = YES;
    searchTextView.backgroundColor = [UIColor whiteColor];
    searchField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
    searchField.delegate = self;
    
    UIButton *searchTypeButton = [[UIButton alloc] initWithFrame:CGRectMake(130, 5, 160, 30)];
    searchTypeButton.backgroundColor = [UIColor colorWithRed:238.0f/255.0f green:238.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
    searchTypeButton.layer.borderColor = [UIColor colorWithRed:33.0f/255.0f green:180.0f/255.0f blue:237.0f/255.0f alpha:1.0f].CGColor;
    searchTypeButton.layer.borderWidth = 1.0f;
    searchTypeButton.layer.cornerRadius = 5;
    searchTypeButton.layer.masksToBounds = YES;
    [searchTypeButton addTarget:self action:@selector(action_ChooseType:) forControlEvents:UIControlEventTouchUpInside];
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(140, 0, 20, 30)];
    image.backgroundColor = [UIColor colorWithRed:33.0f/255.0f green:180.0f/255.0f blue:237.0f/255.0f alpha:1.0f];
    image.image = [UIImage imageNamed:@"三角"];
    searchType = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 140, 30)];
    searchType.text = @"请选择";
    searchType.font = [UIFont systemFontOfSize:13];
    
    searchButton = [[UIButton alloc] initWithFrame:CGRectMake(300, 5, 70, 30)];
    [searchButton setTitle:@"查询" forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
    searchButton.layer.cornerRadius = 5;
    searchButton.layer.masksToBounds = YES;
    searchButton.backgroundColor = [UIColor lightGrayColor];
    searchButton.userInteractionEnabled = NO;
    
    
    [headerView addSubview:searchTextView];
    [searchTextView addSubview:searchField];
    [headerView addSubview:searchTypeButton];
    [searchTypeButton addSubview:image];
    [searchTypeButton addSubview:searchType];
    [headerView addSubview:searchButton];
    
    
    return headerView;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    IntolawyerViewController *ILDC = [[IntolawyerViewController alloc] init];
    ILDC.intoId = self.dataSource[indexPath.row][@"id"];
    ILDC.lawyerName = self.dataSource[indexPath.row][@"name"];
    [self.navigationController pushViewController:ILDC animated:YES];
}
#pragma mark - JHPickerDelegate

-(void)PickerSelectorIndixString:(NSString *)str
{
    NSLog(@"%@",str);
    searchType.text = str;
}

#pragma mark - Getter

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 180;
        [_tableView registerClass:[ChangeLawyerCell class] forCellReuseIdentifier:@"ChangeLawyerCell"];
        
        
    }
    return _tableView;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
